import {
  applyFeedback
} from "../../../api/other";
import config from "../../../config";

// pages/user/feedback/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    api: config.api,
    count: 0,
    intro: '',
    fileList: [],
    uploadHeader: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uploadHeader: {
        token: getApp().globalData.token
      }
    })
  },

  /**
   * 提交建议与意见
   */
  handleCommit() {
    if (this.data.intro === '') {
      wx.showToast({
        title: '请输入反馈内容',
        icon: 'none',
      })
      return
    }

    let imgs = [];
    this.data.fileList.forEach(item => {
      imgs.push(JSON.parse(item.res.data).data);
    })

    applyFeedback({
      content: this.data.intro,
      images: imgs
    }).then(() => {
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      let timer = setTimeout(() => {
        clearTimeout(timer);
        wx.navigateBack({})
      }, 2000);
    })

  },

  onInput(e) {
    this.data.intro = e.detail.value.trim();
    this.setData({
      count: this.data.intro.length
    })
  },

  onChange(e) {
    const {
      file,
      fileList
    } = e.detail
    if (file.status === 'uploading') {
      wx.showLoading()
    }

    this.setData({
      fileList
    })
  },
  onSuccess(e) {
    // console.log('onSuccess', e)
  },
  onFail(e) {
    // console.log('onFail', e)
  },
  onComplete(e) {
    // console.log('onComplete', e)
    wx.hideLoading()
  },
  onPreview(e) {
    // console.log('onPreview', e)
    const {
      file,
      fileList
    } = e.detail
    wx.previewImage({
      current: file.url,
      urls: fileList.map((n) => n.url),
    })
  },
})